<html>
<head><meta charset="utf-8"><title>Dumping/pretty-printing AST/HIR? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html">Dumping/pretty-printing AST/HIR?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="181951993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/181951993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#181951993">(Nov 26 2019 at 18:54)</a>:</h4>
<p>Is there a way to dump/pretty-print (showing rustc types/constructors used) parsed AST or HIR passed to the type checker? I'm looking at some front-end issues and it'd be really helpful if I knew how a program is represented in AST or HIR (ideally both). I checked rustc book but couldn't see anything relevant.</p>



<a name="182026445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182026445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182026445">(Nov 27 2019 at 14:57)</a>:</h4>
<p>Anyone?</p>



<a name="182026994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182026994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182026994">(Nov 27 2019 at 15:03)</a>:</h4>
<p>I think we're removing some of that dumping logic</p>



<a name="182027011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182027011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182027011">(Nov 27 2019 at 15:03)</a>:</h4>
<p>not sure if the hir dumps ever showed types</p>



<a name="182027028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182027028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182027028">(Nov 27 2019 at 15:03)</a>:</h4>
<p>Interesting. Is it not a useful feature for devs? It'd be helpful to me if I could see how my program is represented.</p>



<a name="182027109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182027109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182027109">(Nov 27 2019 at 15:04)</a>:</h4>
<p>You'll want <code>-Zunpretty=hir,typed</code> I think</p>



<a name="182027430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182027430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182027430">(Nov 27 2019 at 15:08)</a>:</h4>
<p>That doesn't show the AST, also is there a way to get a similar output but for type checker input?</p>



<a name="182114508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182114508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182114508">(Nov 28 2019 at 14:53)</a>:</h4>
<p>If anyone else needs this, the AST types implement <code>Debug</code> so I was able to do what I need with a small modification in the parser entry point:</p>
<div class="codehilite"><pre><span></span><span class="gh">diff --git a/src/librustc_parse/parser/module.rs b/src/librustc_parse/parser/module.rs</span>
<span class="gh">index 59d7c2b4239..62df04f859a 100644</span>
<span class="gd">--- a/src/librustc_parse/parser/module.rs</span>
<span class="gi">+++ b/src/librustc_parse/parser/module.rs</span>
<span class="gu">@@ -14,6 +14,8 @@ use errors::PResult;</span>

 use std::path::{self, Path, PathBuf};

<span class="gi">+use log::debug;</span>
<span class="gi">+</span>
 /// Information about the path to a module.
 pub(super) struct ModulePath {
     name: String,
<span class="gu">@@ -35,6 +37,9 @@ impl&lt;&#39;a&gt; Parser&lt;&#39;a&gt; {</span>
             module: self.parse_mod_items(&amp;token::Eof, lo)?,
             span: lo.to(self.token.span),
         });
<span class="gi">+</span>
<span class="gi">+        debug!(&quot;Parsed crate: {:?}&quot;, krate.as_ref().unwrap());</span>
<span class="gi">+</span>
         krate
     }
</pre></div>


<p>I run with <code>RUSTC_LOG=rustc_parse::parser::module rustc --edition 2018 -Zno-codegen foo.rs</code></p>



<a name="182133648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Dumping/pretty-printing%20AST/HIR%3F/near/182133648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Dumping.2Fpretty-printing.20AST.2FHIR.3F.html#182133648">(Nov 28 2019 at 20:14)</a>:</h4>
<p>For HIR there's <code>-Zunpretty=hir-tree</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>